package com.dianping.cat.home.storage.transform;

import com.dianping.cat.home.storage.IEntity;
import com.dianping.cat.home.storage.entity.Link;
import com.dianping.cat.home.storage.entity.Storage;
import com.dianping.cat.home.storage.entity.StorageGroup;
import com.dianping.cat.home.storage.entity.StorageGroupConfig;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;











public class DefaultSaxParser
  extends DefaultHandler
{
/*  35 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  37 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  39 */   private Stack<String> m_tags = new Stack();

/*  41 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  45 */   private StringBuilder m_text = new StringBuilder();


/*  48 */   public static StorageGroupConfig parse(InputStream in) throws SAXException, IOException { return (StorageGroupConfig)parseEntity(StorageGroupConfig.class, new InputSource((InputStream)removeBOM(in))); }



/*  52 */   public static StorageGroupConfig parse(Reader reader) throws SAXException, IOException { return (StorageGroupConfig)parseEntity(StorageGroupConfig.class, new InputSource((Reader)removeBOM(reader))); }



/*  56 */   public static StorageGroupConfig parse(String xml) throws SAXException, IOException { return (StorageGroupConfig)parseEntity(StorageGroupConfig.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  62 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  63 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  65 */       factory.setValidating(false);
/*  66 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  68 */       factory.newSAXParser().parse(is, handler);
/*  69 */       return (T)handler.getEntity();
/*  70 */     } catch (ParserConfigurationException e) {
/*  71 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  76 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  80 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  86 */     if (obj instanceof String) {
/*  87 */       String str = (String)obj;

/*  89 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  90 */         return (T)str.substring(1);
      }
/*  92 */       return obj;
    }
/*  94 */     if (obj instanceof InputStream) {
      BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/*  97 */       in.mark(3);

/*  99 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 100 */         in.reset();
      }

/* 103 */       return (T)in;
/* 104 */     }  if (obj instanceof Reader) {
/* 105 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 107 */       in.mark(1);

/* 109 */       if (in.read() != 65279) {
/* 110 */         in.reset();
      }

/* 113 */       return (T)in;
    }
/* 115 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 121 */     if (value == null || value.length() == 0) {
/* 122 */       return defaultValue;
    }

/* 125 */     if (type == Boolean.class)
/* 126 */       return (T)Boolean.valueOf(value); 
/* 127 */     if (type == Integer.class)
/* 128 */       return (T)Integer.valueOf(value); 
/* 129 */     if (type == Long.class)
/* 130 */       return (T)Long.valueOf(value); 
/* 131 */     if (type == Short.class)
/* 132 */       return (T)Short.valueOf(value); 
/* 133 */     if (type == Float.class)
/* 134 */       return (T)Float.valueOf(value); 
/* 135 */     if (type == Double.class)
/* 136 */       return (T)Double.valueOf(value); 
/* 137 */     if (type == Byte.class)
/* 138 */       return (T)Byte.valueOf(value); 
/* 139 */     if (type == Character.class) {
/* 140 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 142 */     return (T)value;
  }




/* 148 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 153 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 158 */     if (uri == null || uri.length() == 0) {
/* 159 */       Object currentObj = this.m_objs.pop();
/* 160 */       String currentTag = (String)this.m_tags.pop();

/* 162 */       if (currentObj instanceof Link) {
/* 163 */         Link link = (Link)currentObj;

/* 165 */         if ("par".equals(currentTag)) {
/* 166 */           link.addPar(getText());
        }
      }
    }

/* 171 */     this.m_text.setLength(0);
  }


/* 175 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 179 */   protected String getText() { return this.m_text.toString(); }


  private void parseForLink(Link parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 183 */     if ("pars".equals(qName) || "par".equals(qName)) {
/* 184 */       this.m_objs.push(parentObj);
    } else {
/* 186 */       throw new SAXException(String.format("Element(%s) is not expected under link!", new Object[] { qName }));
    }

/* 189 */     this.m_tags.push(qName);
  }

  private void parseForStorage(Storage parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 193 */     this.m_objs.push(parentObj);
/* 194 */     this.m_tags.push(qName);
  }

  private void parseForStorageGroup(StorageGroup parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 198 */     if ("link".equals(qName)) {
/* 199 */       Link link = this.m_maker.buildLink(attributes);

/* 201 */       this.m_linker.onLink(parentObj, link);
/* 202 */       this.m_objs.push(link);
/* 203 */     } else if ("storage".equals(qName)) {
/* 204 */       Storage storage = this.m_maker.buildStorage(attributes);

/* 206 */       this.m_linker.onStorage(parentObj, storage);
/* 207 */       this.m_objs.push(storage);
    } else {
/* 209 */       throw new SAXException(String.format("Element(%s) is not expected under storage-group!", new Object[] { qName }));
    }

/* 212 */     this.m_tags.push(qName);
  }

  private void parseForStorageGroupConfig(StorageGroupConfig parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 216 */     if ("storage-group".equals(qName)) {
/* 217 */       StorageGroup storageGroup = this.m_maker.buildStorageGroup(attributes);

/* 219 */       this.m_linker.onStorageGroup(parentObj, storageGroup);
/* 220 */       this.m_objs.push(storageGroup);
    } else {
/* 222 */       throw new SAXException(String.format("Element(%s) is not expected under storage-group-config!", new Object[] { qName }));
    }

/* 225 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 229 */     if ("storage-group-config".equals(qName)) {
/* 230 */       StorageGroupConfig storageGroupConfig = this.m_maker.buildStorageGroupConfig(attributes);

/* 232 */       this.m_entity = storageGroupConfig;
/* 233 */       this.m_objs.push(storageGroupConfig);
/* 234 */       this.m_tags.push(qName);
/* 235 */     } else if ("storage-group".equals(qName)) {
/* 236 */       StorageGroup storageGroup = this.m_maker.buildStorageGroup(attributes);

/* 238 */       this.m_entity = storageGroup;
/* 239 */       this.m_objs.push(storageGroup);
/* 240 */       this.m_tags.push(qName);
/* 241 */     } else if ("link".equals(qName)) {
/* 242 */       Link link = this.m_maker.buildLink(attributes);

/* 244 */       this.m_entity = link;
/* 245 */       this.m_objs.push(link);
/* 246 */       this.m_tags.push(qName);
/* 247 */     } else if ("storage".equals(qName)) {
/* 248 */       Storage storage = this.m_maker.buildStorage(attributes);

/* 250 */       this.m_entity = storage;
/* 251 */       this.m_objs.push(storage);
/* 252 */       this.m_tags.push(qName);
    } else {
/* 254 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 260 */     if (uri == null || uri.length() == 0) {
/* 261 */       if (this.m_objs.isEmpty()) {
/* 262 */         parseRoot(qName, attributes);
      } else {
/* 264 */         Object parent = this.m_objs.peek();
/* 265 */         String tag = (String)this.m_tags.peek();

/* 267 */         if (parent instanceof StorageGroupConfig) {
/* 268 */           parseForStorageGroupConfig((StorageGroupConfig)parent, tag, qName, attributes);
/* 269 */         } else if (parent instanceof StorageGroup) {
/* 270 */           parseForStorageGroup((StorageGroup)parent, tag, qName, attributes);
/* 271 */         } else if (parent instanceof Link) {
/* 272 */           parseForLink((Link)parent, tag, qName, attributes);
/* 273 */         } else if (parent instanceof Storage) {
/* 274 */           parseForStorage((Storage)parent, tag, qName, attributes);
        } else {
/* 276 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 280 */       this.m_text.setLength(0);
    } else {
/* 282 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/storage/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */